package com.veepeak.server.api.web;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.veepeak.server.api.web.vo.ObdVO;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.Obd;
import com.veepeak.server.domain.User;
import com.veepeak.server.domain.UserObd;
import com.veepeak.server.dto.PageModel;
import com.veepeak.server.dto.mapper.ObdMapper;
import com.veepeak.server.service.ObdService;
import com.veepeak.server.service.UserObdService;
import com.veepeak.server.service.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/web/obd")
@Api(description = "obd接口")
public class ObdWebApi {
    @Autowired
    private ObdService obdService;
    @Autowired
    private ObdMapper obdMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserObdService userObdService;

    @PostMapping(name = "")
    public JSONObject list(PageModel pageModel, String status, HttpServletRequest request){
        String searchValue = request.getParameter("search[value]");

        Page<Obd> obds = null;
        if ( StringUtils.isNotBlank(status)){
            obds = obdService.findPageByStatus(pageModel.getPage(), pageModel.getLength(), Integer.parseInt(status));
        }else {
            obds = obdService.datatables(pageModel.getPage(), pageModel.getLength(), searchValue);
        }

        Page<ObdVO> result = obds.map(obd -> {
            ObdVO vo = obdMapper.toVo(obd);
//            List<UserObd> userObds = userObdService.findByUserId(obd.getId());
//            if(userObds!=null && userObds.size()>0){
//                vo.setStatus(ObdVO.STATUS_BIND);
//            }else{
//                vo.setStatus(ObdVO.STATUS_UNBIND);
//            }
            return vo;
        });

        return JSONResult.toSuccessJson(new PageModel(pageModel.getDraw(),result),"").getJSONObject("data");
    }

//    @PostMapping(name = "/status/{status}")
//    public JSONObject findByStatus(PageModel pageModel,String searchValue, @PathVariable String status){
//        System.out.println("----->  "+status);
//        Page<Obd> obds = null;
//        if ( Integer.parseInt(status) != 99) {
//            obds = obdService.findPageByStatus(pageModel.getPage(), pageModel.getLength(), Integer.parseInt(status));
//        }else{
//            obds = obdService.datatables(pageModel.getPage(),pageModel.getLength(),null);
//        }
//
//        Page<ObdVO> result = obds.map(obd -> {
//            ObdVO vo = obdMapper.toVo(obd);
//            return vo;
//        });
//
//        return JSONResult.toSuccessJson(new PageModel(pageModel.getDraw(),result),"").getJSONObject("data");
//    }
}
